package com.yanq.liancar.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.adapter.SmallBGoodsListAdapter;
import com.yanq.liancar.bean.BShopGoodsListBean;
import com.yanq.liancar.bean.BannerBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.GlideImageLoader;
import com.yanq.liancar.util.LogUtils;
import com.yanq.liancar.util.Utils;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SmallBEJGoodsListActivity extends BaseToolbarActivity {
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.ll_search)
    LinearLayout llSearch;
    @BindView(R.id.goodslistview)
    RecyclerView goodslistview;
    @BindView(R.id.smartrefreshlayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.banner)
    Banner banner;

    private List<BShopGoodsListBean.DataBean.ListBean> goodsListBeans = new ArrayList<>();
    private SmallBGoodsListAdapter adapter;
    private int page = 1;
    private boolean isClear = true;
    private int type;
    private int pages;
    private String where;
    private int Bid, Sid;
    private List<BannerBean.DataBean> bannerList=new ArrayList<>();
    private List<String> imageList=new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_small_b_goodslist);
        Utils.setStatusBar(this, true, false);
        ButterKnife.bind(this);
        mIvRight.setVisibility(View.GONE);
        type = getIntent().getExtras().getInt("type");
        Bid = getIntent().getExtras().getInt("Bid");
        Sid = getIntent().getExtras().getInt("Sid");
        mTvTitle.setText("商品列表");
        LogUtils.e("afhahfakl", type + "-----" + Bid);

        refreshListener();
        initRecyclerView();
        initOkgo();
        initOkgoBanner();
    }

    private void initBannerData() {
        LogUtils.e("adasdadasd",imageList.size()+"");
        banner.setImageLoader(new GlideImageLoader())
                .setImages(imageList)//设置图片集合
                .setDelayTime(3000)//设置轮播时间
                .setBannerStyle(BannerConfig.CIRCLE_INDICATOR)//设置指示器位置（当banner模式中有指示器时）
                .setIndicatorGravity(BannerConfig.CENTER)
                .start();//banner设置方法全部调用完毕时最后调用
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                //ToastUtils.showToast(SmallBGoodsDetailsActivity.this, "您点击了第" + (mBanners.get(position).type + 1) + "张照片");
            }
        });
        //开始轮播
        banner.startAutoPlay();
    }

    private void initRecyclerView() {
        goodslistview.setNestedScrollingEnabled(false);
        goodslistview.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new SmallBGoodsListAdapter(R.layout.item_small_b_goodslist, goodsListBeans);
        goodslistview.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putInt("goodsID", goodsListBeans.get(position).getGoodsid());
                RxActivityTool.skipActivity(mContext, SmallBGoodsDetailsActivity.class, bundle);
            }
        });
    }

    @OnClick({R.id.iv_right, R.id.ll_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_right:
                break;
            case R.id.ll_search:
                Bundle bundle = new Bundle();
                bundle.putString("where", "SmallBEJGoodsListActivity");
                bundle.putInt("Bid", Bid);
                bundle.putInt("Sid", Sid);
                RxActivityTool.skipActivity(mContext, SmallBSearchActivity.class, bundle);
                break;
        }
    }

    private void refreshListener() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                isClear = true;
                //网络请求
                initOkgo();
                refreshLayout.finishRefresh();
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                isClear = false;
                page++;
                //网络请求
                LogUtils.e("dadasdada", pages + "=====" + page);
                if (pages >= page) {
                    initOkgo();
                }
                refreshLayout.finishLoadMore();
            }
        });
    }

    private void initOkgo() {
        OkGo.<String>post(Config.s + "llc/api/selectSmallClassifyGoods")
                .tag(this)
                .params("bigclassifyid", Bid)
                .params("small_classify", Sid)
                .params("pageNum", page)
                .params("pageSize", 7)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        BShopGoodsListBean selectClassifyBean = gson.fromJson(response.body(), BShopGoodsListBean.class);
                        if (selectClassifyBean.isIsSuccess()) {
                            pages = selectClassifyBean.getData().getPages();
                            if (isClear) {
                                goodsListBeans.clear();
                            }
                            goodsListBeans.addAll(selectClassifyBean.getData().getList());
                            adapter.setNewData(goodsListBeans);
                        } else {
                            RxToast.error(selectClassifyBean.getOutputMessage());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        RxToast.error("服务器连接错误");
                    }
                });
    }


    private void initOkgoBanner() {
        OkGo.<String>post(Config.s + "llc/api/b2ShopGoodsTopLunBo")
                .tag(this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        BannerBean bannerBean = gson.fromJson(response.body(), BannerBean.class);
                        if (bannerBean.isIsSuccess()) {
                            if (bannerBean.getCode()==200){
                                bannerList.clear();
                                bannerList.addAll(bannerBean.getData());
                                for (int i = 0; i <bannerList.size() ; i++) {
                                    imageList.add(bannerList.get(i).getWebPath());
                                }
                                initBannerData();
                            }
                        } else {
                            RxToast.error(bannerBean.getOutputMessage());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        RxToast.error("服务器连接错误");
                    }
                });
    }

    //如果你需要考虑更好的体验，可以这么操作
    @Override
    protected void onStart() {
        super.onStart();
        //开始轮播
        banner.startAutoPlay();
    }

    @Override
    protected void onStop() {
        super.onStop();
        //结束轮播
        banner.stopAutoPlay();
    }

}
